window.airGam = window.airGam || {};
airGam.GameCont = function () {
    var s = this;
    annie.Sprite.call(s);
    /*_a2x_need_start*/s.addPoint=null;s.kmBox=null;s.plan=null;s.sound=null;s.starBox=null;/*_a2x_need_end*/
    annie.initRes(s, "airGam", "GameCont");
    var starArr = [new airGam.Star1(), new airGam.Star2(), new airGam.Star3(), new airGam.Star4(), new airGam.Star5(),
        new airGam.Star1(), new airGam.Star2(), new airGam.Star3(), new airGam.Star4(), new airGam.Star5(),
        new airGam.Star1(), new airGam.Star2(), new airGam.Star3(), new airGam.Star4(), new airGam.Star5()];
    var starR = [80, 45, 60, 65, 50,
        80, 45, 60, 65, 50,
        80, 45, 60, 65, 50];
    var showStarR = [50];
    var bgMusci = new annie.Sound("resource/bgm.MP3");
    var flyMusci = new annie.Sound("resource/fly.mp3");
    var boomMusci = new annie.Sound("resource/boom.mp3");
    var niceMusci = new annie.Sound("resource/nice.MP3");
    var arriveMusci = new annie.Sound("resource/one.mp3");
    var viewWidth;
    var starBox = [s.starBox.earth];
    var starY = [1200, 875, 530, 142];
    annie.kmNum = 0;
    var boom = false;
    var h = 0,
        clickNum,
        start = false,
        dis = 0,
        nowStar = 0,
        Num = 0,
        ufoRotationSpeed = 0,
        canPlayMusic = true,
        addKm = 0,
        rotationSpeed = 0;

    //添加新的星球
    function addStar() {
        var chooseX = Math.ceil(Math.random() * 2);
        for (var i = 1; i < 4; i++) {
            Num = Math.ceil(Math.random() * (starArr.length - 1));
            if (chooseX == 1) {
                if (i % 2 == 0) {
                    var pointX = Math.ceil(Math.random() * (viewWidth / 2 - 80)) + 100;
                } else {
                    var pointX = Math.ceil(Math.random() * (viewWidth / 2 - 190)) + (viewWidth / 2) + 80;
                }
            } else {
                if (i % 2 == 0) {
                    var pointX = Math.ceil(Math.random() * (viewWidth / 2 - 190)) + (viewWidth / 2) + 80;
                } else {
                    var pointX = Math.ceil(Math.random() * (viewWidth / 2 - 80)) + 100;
                }
            }
            starBox[i] = starArr[Num];//随机星球样式
            showStarR[i] = starR[Num];//随机星球半径
            starBox[i].x = pointX; //随机星球x轴坐标
            starBox[i].y = 1340 - (400 * i); //随机星球y轴坐标
            starArr.splice(Num, 1);
            starR.splice(Num, 1);
            //将选中的半径和星球样式从初始数组中移除
            s.starBox.addChild(starBox[i]);
        }
    }

    //背景音效控制按钮
    s.sound.addEventListener(annie.MouseEvent.CLICK, function (e) {
        if (s.sound.currentFrame == 1) {
            canPlayMusic = false;
            bgMusci.pause(true);
            flyMusci.stop();
            boomMusci.stop();
            niceMusci.stop();
            arriveMusci.stop();
            s.sound.gotoAndStop(2)
        } else {
            canPlayMusic = true;
            bgMusci.pause(false);
            s.sound.gotoAndStop(1)
        }
    })

    s.addEventListener(annie.Event.ADD_TO_STAGE, function (e) {
        s.sound.mouseChildren = false;
        viewWidth = s.stage.viewRect.width;
        bgMusci.play(0, 9999)
        addStar();
    })
    s.addEventListener(annie.Event.ENTER_FRAME, function (e) {
        rotationSpeed += 6;
        ufoRotationSpeed += 6;
        //星球自转
        for (var a = 0; a < starBox.length; a++) {
            starBox[a].rotation = rotationSpeed;
        }
        if (boom) {
            //飞船沿射线方向直线运动
            s.plan.x = s.plan.x + Math.sin(h) * dis;
            s.plan.y = s.plan.y + (-Math.cos(h)) * dis;
            dis++;
        } else {
            //飞船绕着当前星球做运动
            a = ufoRotationSpeed * Math.PI / 180;
            s.plan.rotation = ufoRotationSpeed + 90;
            s.plan.x = starBox[nowStar].x + Math.cos(a) * (showStarR[nowStar] + 20);
            s.plan.y = starBox[nowStar].y + Math.sin(a) * (showStarR[nowStar] + 20);
        }
        if (start) {
            //飞船飞出屏幕游戏结束
            if (s.plan.x > 850 || s.plan.x < -100 || s.plan.y < -200 || s.plan.y > 1400) {
                if (canPlayMusic) {
                    boomMusci.play()
                }
                s.plan.gotoAndStop(1);
                h = 0;
                dis = 0;
                rotationSpeed = 0;
                annie.globalDispatcher.dispatchEvent("onChangeCont", {cont: "listPage"});
                bgMusci.stop();
                start = false;
            }
            if (boom) {
                for (var i = 1; i < starBox.length; i++) {
                    //判断飞船是否到达下一个星球
                    if (annie.Point.distance(s.plan.x, s.plan.y, starBox[i].x, starBox[i].y) < (30 + showStarR[i])) {
                        dis = 0;
                        boom = false;
                        var del;
                        del = i;
                        if (canPlayMusic) {
                            flyMusci.stop();
                            if (del >= 2) {
                                niceMusci.play();
                            } else {
                                arriveMusci.play();
                            }
                        }
                        s.plan.gotoAndStop(1);
                        nowStar = del;
                        ufoRotationSpeed = Math.atan2((s.plan.y - starBox[del].y), (s.plan.x - starBox[del].x)) * 180 / Math.PI;
                        //飞船降落在第几个星球上，生成新的星球，星球与飞船下移，使飞船每次都在最下方
                        for (var o = 4; o < 4 + del; o++) {
                            var nowNum = Math.ceil(Math.random() * (starArr.length - 1));
                            starBox.push(starArr[nowNum]);
                            showStarR.push(starR[nowNum]);
                            if (starBox[3].x > viewWidth / 2) {
                                if (o % 2 == 0) {
                                    starBox[o].x = Math.ceil(Math.random() * (viewWidth / 2 - 80)) + 100;
                                } else {
                                    starBox[o].x = Math.ceil(Math.random() * (viewWidth / 2 - 190)) + (viewWidth / 2) + 80;
                                }
                            } else {
                                if (o % 2 == 0) {
                                    starBox[o].x = Math.ceil(Math.random() * (viewWidth / 2 - 190)) + (viewWidth / 2) + 80;
                                } else {
                                    starBox[o].x = Math.ceil(Math.random() * (viewWidth / 2 - 80)) + 100;
                                }
                            }
                            starBox[o].y = 1340 - (400 * o);
                            s.starBox.addChild(starBox[o]);
                            starArr.splice(nowNum, 1)
                            starR.splice(nowNum, 1)
                        }
                        for (var v = 0; v < del; v++) {
                            annie.Tween.to(starBox[v], 0.5, {y: 1600})
                        }

                        for (var a = del; a < starBox.length; a++) {
                            var moveY = starY[a - del];
                            annie.Tween.to(starBox[a], 0.5, {
                                y: moveY, onComplete: function (e) {
                                    if (start) {
                                        //飞船落在第几个星球上，增加相应的分数
                                        if (del == 1) {
                                            annie.kmNum += 5;
                                            addKm = 5;
                                        } else if (del == 2) {
                                            annie.kmNum += 15;
                                            addKm = 15;
                                        } else {
                                            annie.kmNum += 40;
                                            addKm = 40;
                                        }
                                        s.addPoint.x = starBox[del].x-110;
                                        s.addPoint.y = starBox[del].y-200;
                                        s.addPoint.pointTextBox.pointText.text = "+" + addKm;
                                        s.addPoint.play();//加分数动画
                                        s.kmBox.kmTxt.km.text = annie.kmNum;
                                        //将移出屏幕的星球移除舞台，并重新添加到星球数组中
                                        for (var q = clickNum; q < del; q++) {
                                            s.starBox.removeChild(starBox[q]);
                                            starArr.push(starBox[q]);
                                            starR.push(showStarR[q]);
                                        }
                                        //将移出屏幕的星球移出星球数组
                                        starBox.splice(0, del);
                                        showStarR.splice(0, del);
                                        nowStar = 0;
                                        start = false;
                                    }
                                }
                            });
                        }
                    }
                }
            }
        }
    })

    //点击屏幕改变飞船状态并使飞船沿切线方向直线飞出
    s.addEventListener(annie.MouseEvent.MOUSE_DOWN, function (e) {
        if (e.target.name == "sound") {

        } else {
            if (!start) {
                if (!clickNum) {
                    clickNum = 1;
                } else {
                    clickNum = 0;
                }
                if (canPlayMusic) {
                    flyMusci.play()
                }
                s.plan.gotoAndStop(2);
                start = true;
                boom = true;
                h = s.plan.rotation * Math.PI / 180;
            }
        }
    })
};
A2xExtend(airGam.GameCont, annie.Sprite);
